package com.bird.ecmainmenu3.model;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bird.ecmainmenu3.R;
import com.bird.ecmainmenu3.base.OnItemClickListener;
import com.bird.ecmainmenu3.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> {
    private static final String TAG = Utils.TAG + AppListAdapter.class.getSimpleName();

    private final OnItemClickListener mOnItemClickListener;
    private final List<AppInfo> mApps;

    public AppListAdapter(List<AppInfo> appList,OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
        this.mApps = appList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position < 0 || position >= getItemCount()) {
            Log.e(TAG, "onBindViewHolder: position=" + position);
        } else {
            Log.d(TAG, "onBindViewHolder: position=" + position);
            if (mApps == null || mApps.isEmpty()) {
                Log.i(TAG, "onBindViewHolder: appList is null");
                return;
            }
            AppInfo appInfo = mApps.get(position);
            if (appInfo != null) {
                Drawable drawable = appInfo.getDrawable();
                holder.icon.setImageDrawable(drawable);
                Log.d(TAG, "onBindViewHolder: setImageResource");

                String title = appInfo.getName();
                Log.d(TAG, "onBindViewHolder: title=" + title);
                holder.title.setText(title);

                Log.d(TAG, "onBindViewHolder: setOnClickListener");
                holder.itemView.setOnClickListener(v -> {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                });
            } else {
                Log.e(TAG, "onBindViewHolder: appInfo is null");
            }
        }
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (mApps != null) {
            size = mApps.size();
            Log.d(TAG, "getItemCount: " + size);
        }
        return size;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateAppInfoList(List<AppInfo> appList) {
        if (appList != null && !appList.isEmpty()) {
            List<AppInfo> apps = new ArrayList<>();
            apps.addAll(appList);

            this.mApps.clear();
            this.mApps.addAll(apps);
            notifyDataSetChanged();

            int size = mApps.size();
            Log.d(TAG, "updateAppInfoList: " + size);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView title;

        ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon_id);
            title = itemView.findViewById(R.id.title_id);
        }
    }
}
